import gtk
import re
from . import log
from . import exceptions
from . import settings
from . import path
from . import strunicode as su

top_parent = None

def set_main_parent(parent_window):
    global top_parent
    
    top_parent = parent_window
    
def error(message, format=_("The following errors occured"), parent_window=None, title=_("Error")):
    """
        Display error message
    """
    if not top_parent is None:
        parent_window = top_parent
        
    dialog = gtk.MessageDialog(parent_window, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR,
        gtk.BUTTONS_OK, message_format=format)
    dialog.format_secondary_text(message)
    dialog.set_title(title)
    dialog.run()
    dialog.destroy()
    
def information(message, parent_window=None, title=_("Information")):
    """
    Display message box.
    """
    if not top_parent is None:
        parent_window = top_parent
        
    dialog = gtk.MessageDialog(parent_window, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO,
        gtk.BUTTONS_OK)
    dialog.format_secondary_text(message)
    dialog.set_title(title)
    dialog.run()
    dialog.destroy()

def confirmation(message, parent_window=None):
    """
    Display message with two buttons "Yes", "No"
    
    @return: gtk.RESPONSE_YES or gtk.RESPONSE_NO 
    """
    if not top_parent is None:
        parent_window = top_parent
        
    dialog = gtk.MessageDialog(parent_window, gtk.DIALOG_MODAL, gtk.MESSAGE_QUESTION,
        gtk.BUTTONS_YES_NO)
    dialog.format_secondary_text(message)
    ret = dialog.run()
    dialog.destroy()
    
    if int(gtk.RESPONSE_YES) == ret:
        ret = gtk.RESPONSE_YES
    else:
        ret = gtk.RESPONSE_NO
        
    return ret

def string_prompt(window_title, label_text, default_value="", parent_window=None):
    """
    Display dialog box with string prompt.
    
    @param window_title: dialog window title
    @param label_text: label text
    @param default_value: default value in the text entry
    @param parent_window: parent window
    @return: entered text or None if user clicked Cancel
    """
    xml = gtk.glade.XML(path.get_data_path("pybookshelf.glade"), root="string_prompt_dialog",
        domain="pybookshelf")
    
    dialog = xml.get_widget("string_prompt_dialog")
    xml.get_widget("pre_text_label").set_text("")
    xml.get_widget("text_label").set_text(label_text)
    xml.get_widget("text_input_entry").set_text(default_value)
    dialog.set_title(window_title)
    
    response = dialog.run()
    
    entry_text = su.ustr2unicode(xml.get_widget("text_input_entry").get_text())
    dialog.destroy()
    
    if response == 0:
        return None
    
    return entry_text
    
def detailed_message_box(title, message, detailed_message, parent_window=None):
    """
    Display message box with additional initially hidden text field.
    """
    xml = gtk.glade.XML(path.get_data_path("pybookshelf.glade"), 
        root="detailed_message_dialog", domain="pybookshelf")
    
    dialog = xml.get_widget("detailed_message_dialog")

    width = settings.get_int("ui/detailed_msgdialog_width")
    height = settings.get_int("ui/detailed_msgdialog_height")
    
    dialog.set_title(title)
    xml.get_widget("message_label").set_label(message)
    dialog.resize(width, height)
    dialog.set_transient_for(parent_window)
    
    buffer = gtk.TextBuffer()
    buffer.set_text(detailed_message)
    xml.get_widget("details_textview").set_buffer(buffer)
    xml.get_widget("close_button").grab_focus()
    
    def on_show_details(widget):
        # show details_textview
        xml.get_widget("details_textview").show()
        xml.get_widget("details_hbox").hide()
    
    xml.get_widget("show_details_button").connect("clicked", on_show_details)
    
    dialog.run()
    width, height = dialog.get_size()
    dialog.destroy()
    
    settings.set_int("ui/detailed_msgdialog_width", width)
    settings.set_int("ui/detailed_msgdialog_height", height)
    

def smart_join(*args):
    s = []
    for a in args:
        if a is None or "" == str(a):
            continue
        s.append(str(a))

    return u" ".join(s)